from typing import Dict, Any
from myProfile.models.data.myProfile_models import Signup


class SignupRepository:

    def __init__(self, engine):
        self.engine = engine

    async def insert_signup(self, details: Dict[str, Any]) -> bool:
        try:
            signup = Signup(**details)
            await self.engine.save(signup)
        except Exception as e:
            print(e)
            return False
        return True

    async def update_signup(self, id: int, details: Dict[str, Any]) -> bool:
        try:
            signup = await self.engine.find_one(Signup, Signup.id == id)
            for key, value in details.items():
                if value is not None and key != 'id':
                    setattr(signup, key, value)
            print(f'signup={signup}')
            await self.engine.save(signup)
        except Exception as e:
            print(e)
            return False
        return True

    async def delete_signup(self, id: int) -> bool:
        try:
            signup = await self.engine.find_one(Signup, Signup.id == id)
            await self.engine.delete(signup)
        except Exception:
            return False
        return True

    async def get_signup(self, id: int) -> Signup:
        signup = await self.engine.find_one(Signup, Signup.id == id)
        return signup

    async def get_all_signup(self) -> list:
        signups = await self.engine.find(Signup)
        return signups

    async def get_signup_username(self, username: str) -> Signup:
        signup = await self.engine.find_one(
            Signup, Signup.username == username)
        return signup
